# Import libraries
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from sklearn.metrics import accuracy_score
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.pyplot import figure
figure(figsize=(5, 20), dpi=300)
from sklearn import preprocessing
from sklearn import manifold
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import pandas as pd
from sklearn.metrics import classification_report
from collections import Counter
from sklearn.datasets import make_classification
from imblearn.over_sampling import SMOTE
from imblearn.combine import SMOTEENN
from sklearn.model_selection import cross_validate
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import GradientBoostingClassifier
from plotnine import ggplot, aes, geom_line, geom_abline, ggtitle, xlab, ylab
from sklearn.preprocessing import MinMaxScaler
import random
from sklearn import metrics
import plotly.express as px
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from sklearn.linear_model import LogisticRegression
import warnings
warnings.filterwarnings('ignore')
<Figure size 1500x6000 with 0 Axes>
# Load data
features = pd.read_csv("Features.csv")
labels = pd.read_csv("Target.CSV")
Concatenating columnwise
data = pd.concat([features, labels], axis=1)
Replacing and Eliminating certain class value
data['BlcaGrade'] = data['BlcaGrade'].replace('Grade I', 'Stage I')
data['BlcaGrade'] = data['BlcaGrade'].replace('Grade II', 'Stage II')
data['BlcaGrade'] = data['BlcaGrade'].replace('Grade III', 'Stage III')
data['BlcaGrade'] = data['BlcaGrade'].replace('Grade IV', 'Stage IV')
# Replacing
data['BlcaGrade'] = data['BlcaGrade'].replace('Stage I', 'Stage II')
# Eliminating
data = data.drop(data[(data['BlcaGrade'] == 'Stage III')].index)
data['BlcaGrade'].value_counts()
label_names = ['Stage II', 'Stage IV']
Encoding Labels
0: Stage II
1: Stage IV
# label_encoder
label_encoder = preprocessing.LabelEncoder()
data['BlcaGrade']= label_encoder.fit_transform(data['BlcaGrade'])
data['BlcaGrade'].value_counts()
1 142 0 138 Name: BlcaGrade, dtype: int64
Duplicate entry checking
# Outputs the total number of rows in the dataframe.
print("Total entries: ", len(data))
# 'duplicates = df.duplicated()' uses the duplicated method to create a boolean series indicating whether each row is a duplicate or not.
duplicates = data.duplicated()
# 'duplicate_rows = df[duplicates]' uses the boolean series to index the dataframe and obtain a sub-dataframe containing only the duplicate rows.
duplicate_rows = data[duplicates]
# Outputs the number of duplicate rows in the sub-dataframe.
print("Duplicate entries: ", len(duplicate_rows))
Total entries: 280 Duplicate entries: 0
NULL entry checking
print("NULL Entries: ", data.isnull().sum().sum())
NULL Entries: 0
# dimensionality reduction using t-SNE
tsne = manifold.TSNE(n_components=2, random_state=42)
# fit and transform
mnist_tr = tsne.fit_transform(data.drop('BlcaGrade',axis=1))
# create dataframe
cps_df = pd.DataFrame(columns=['Component 1', 'Component 2', 'target'], data=np.column_stack((mnist_tr, data['BlcaGrade'])))
# cast targets column to int
cps_df.loc[:, 'target'] = cps_df.target.astype(int)
fig = px.scatter(
cps_df, x='Component 1', y='Component 2',
color=cps_df.target.astype(str), labels={'color': 'Target Variable'}, width=600, height=400, title="t-SNE plot for Stage II vs Stage IV")
fig.show()
from sklearn.feature_selection import mutual_info_classif
from sklearn.feature_selection import SelectKBest
X = data.drop('BlcaGrade',axis=1)
y = data['BlcaGrade']
mutual_info = mutual_info_classif(X, y)
mutual_info = pd.Series(mutual_info)
best_cols = SelectKBest(mutual_info_classif, k=100)
best_cols.fit(X, y)
print((X.columns[best_cols.get_support()]))
reducedFeatures = pd.DataFrame(X.columns[best_cols.get_support()])
reducedFeatures.to_csv("ReducedFeatures.csv")
selectedFeatures = list(X.columns[best_cols.get_support()])
Index(['ADIPOR2', 'MYOC', 'VCL', 'MAGEC2', 'MRPS10', 'PARP12', 'NFYC', 'PTGS2',
'TP53INP2', 'CD59', 'TMEM40', 'RGS1', 'SORBS1', 'HNRNPM', 'RANBP1',
'SPINT3', 'PGK1', 'TLE5', 'HSPB1', 'RARRES2', 'CXCL12', 'UNC5B',
'ALDH3A1', 'BST1', 'ATP5F1B', 'BCL6', 'NR4A3', 'CXCR4', 'CD244', 'SOX4',
'ERGIC3', 'IDO1', 'RAB25', 'HMGCS2', 'FST', 'ARGLU1', 'ETS1', 'EHF',
'CKAP4', 'DDX56', 'IL36RN', 'SLCO2B1', 'RAB15', 'RETREG3', 'S100A8',
'RAB5A', 'SCUBE3', 'INA', 'MMP3', 'C11orf53', 'BATF', 'CLIC6', 'TMEM69',
'TLCD1', 'KCNF1', 'RPL22L1', 'C4orf3', 'GEM', 'PSIP1', 'HNRNPK', 'CCT2',
'GPX4', 'TUBA1A', 'FEN1', 'TMEM129', 'CRCT1', 'FOS', 'PFKFB3', 'RPS9',
'PWWP2B', 'CYP4F11', 'GNG12', 'RNF213', 'SNHG29', 'SFN', 'MYLPF',
'NAA38', 'FHL3', 'H2AC20', 'SNN', 'KRT10', 'HES4', 'SUMO2', 'NCOA6',
'C2orf72', 'MUC21', 'PSMB10', 'MT-TC', 'IGHJ6', 'EMP2', 'UBA52',
'IGHV4-59', 'NDUFAF8', 'MTCO2P12', 'GSTA1', 'APOBEC3C', 'CWC25', 'EPOP',
'H2AC8', 'H3C3'],
dtype='object')
score = list(best_cols.scores_)
score.sort(reverse=True)
score[:100]
# Figure Size
fig = plt.figure(figsize =(10, 7))
# Horizontal Bar Plot
plt.bar(best_cols.get_feature_names_out(X.columns)[:20], score[:20])
plt.xticks(rotation = 45)
# Show Plot
plt.show()
X = data[selectedFeatures].values
y = data['BlcaGrade'].values
k = len(y)/3 # Define the split size of outer cv here
k = int(k)
# Train 1, Train 2, Test 3 - Outer CV 1
outerFold_features_1 = X[:k]
outerFold_labels_1 = y[:k]
# Train 1, Test 2, Train 3 - Outer CV 2
outerFold_features_2 = X[k:2*k]
outerFold_labels_2 = y[k:2*k]
# Test 1, Train 2, Train 3 - Outer CV 3
outerFold_features_3 = X[2*k:(3*k)+1]
outerFold_labels_3 = y[2*k:(3*k)+1]
# Training Features and Labels for 1st Outer CV
features_1 = np.concatenate([outerFold_features_1, outerFold_features_2])
label_1 = np.concatenate([outerFold_labels_1, outerFold_labels_2])
# Training Features and Labels for 2nd Outer CV
features_2 = np.concatenate([outerFold_features_1, outerFold_features_3])
label_2 = np.concatenate([outerFold_labels_1, outerFold_labels_3])
# Training Features and Labels for 3rd Outer CV
features_3 = np.concatenate([outerFold_features_2, outerFold_features_3])
label_3 = np.concatenate([outerFold_labels_2, outerFold_labels_3])
# ---------------------------------------------------------------------------------------------------------------
# This function prints a formatted string to the console with information about the current iteration in inner CV
# ---------------------------------------------------------------------------------------------------------------
def disp(count, feature, p1, p2, trainResult, testResult, selected, clf_arguments1, clf_arguments2):
''' This function prints a formatted string to the console with information about the current iteration in inner CV
args: (9 arguments)
count - Iteration count
feature - Particularly the best feature
p1 - One of the parameter value for classifier
p2 - Another parameter value for classifier
trainResult - Mean training accuracy of a particular iteration in inner cv
testResult - Mean test accuracy of a particular iteration in innver cv
selected - Global variable reference 'featuresOuterFold'; It stores the best features as the iteration goes on for inner cv
clf_arguments1 - Name for one of the parameter of classifier
clf_arguments2 - Name for another parameter of classifier
Returns:
No return value
'''
print("Iteration " + str(count) + " >> Feature: " + str(feature) + "; " + clf_arguments1 + ": " + str(p1) + "; " + clf_arguments2 + ": " + str(p2) + "; Train Accuracy: " + str(round(trainResult, 4)) + "; Test Accuracy: " + str(round(testResult, 4)) + "; Selected Features: " + str(selected))
print("---------------------------------------------------------------------------------------------------------------------------------")
# ----------------------------------------------------------------
# This function is used to perform inner cross-validation with FFS
# ----------------------------------------------------------------
def innerCV(count, features, param1, param2, X, y, cv, clf, clf_arguments1, clf_arguments2):
'''
This function is used to perform inner cross-validation on a machine learning algorithm, where two parameters are being optimized.
The function loops through all possible combinations of parameter values and feature indices.
args:
count - Iteration count
feautres - Reduced features from Mutual Information
param1 - One of the parameter value for classifier
param2 - Another parameter value for classifier
X - Features of a particular outerfold
y - Target Variable of a particular outerfold
cv - K value for inner cv
clf - Class name of classifier
clf_arguments1 - Name for one of the parameter of classifier
clf_arguments2 - Name for another parameter of classifier
Returns:
feature - The best feature index found in the loop
'''
temp = 0.0
for i in features:
for j in param1:
for k in param2:
# This line creates a dictionary called args containing the two parameter values being tested in this iteration of the loop
args = {clf_arguments1:j, clf_arguments2:k}
# This line uses scikit-learn's cross_validate function to perform cross-validation on the machine learning algorithm being tested (clf)
# It passes in the args dictionary as the parameters to the algorithm
scores = cross_validate(clf(**args), X[:, i - 1].reshape(-1, 1), y, cv=cv, return_train_score=True)
# This line checks if the current test score is greater than or equal to the current best test score
if temp <= (float)(scores['test_score'].mean()):
# This line updates the best test score to be the current test score
temp = (float)(scores['test_score'].mean())
# This line stores the mean training score for the current iteration
trainResult = scores['train_score'].mean()
# This line stores the mean test score for the current iteration
testResult = scores['test_score'].mean()
# Select the best feature in each iteration
feature = i
# Select the best parameters in each iteration
p1 = j
# Select the best parameters in each iteration
p2 = k
# This line appends the current best feature to a global list
featuresOuterFold.append(feature)
# This line calls a function called disp to print out information about the current iteration of the program
disp(count, feature, p1, p2, trainResult, testResult, featuresOuterFold, clf_arguments1, clf_arguments2)
# This line returns the best feature index found in the loop
return feature
# ------------------------------------------------------------------------------
# This function implements the outer loop of the nested cross-validation process
# ------------------------------------------------------------------------------
def outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, X, y):
'''
A function to perform the outer fold cross-validation by iterating over the features and hyperparameters to find the best combination of both.
The function takes the following arguments:
args:
clf - Class name of classifier
clf_arguments1 - Name for one of the parameter of classifier
clf_arguments2 - Name for another parameter of classifier
param1 - One of the parameter value for classifier
param2 - Another parameter value for classifier
X - Features of a particular outerfold
y - Target Variable of a particular outerfold
Returns:
No return value
'''
## Create a list of reduced feature numbers from 1 to the total number of selected features
features = [i+1 for i in range(len(selectedFeatures))]
# Remove the last parameter value from params1 list and assign it to param1 variable
param1 = params1.pop()
# Remove the last parameter value from params2 list and assign it to param2 variable
param2 = params2.pop()
# Loop through the outer fold cross-validation iterations, from 1 to 5 (inclusive)
for i in range(1, 6):
if i == 1:
# Call the innerCV function with the first set of features and hyperparameters
feature = innerCV(i, features, param1, param2, X, y, 5, clf, clf_arguments1, clf_arguments2)
else:
# Remove the previously selected feature from the features list
features.remove(feature)
# Remove the last parameter value from params1 list and assign it to param1 variable
param1 = params1.pop()
# Remove the last parameter value from params2 list and assign it to param2 variable
param2 = params2.pop()
# Call the innerCV function with the updated set of features and hyperparameters
# Store the best feature in the feature variable
feature = innerCV(i, features, param1, param2, X, y, 5, clf, clf_arguments1, clf_arguments2)
# -------------------------------------------------------------------------------------------------------
# This function is used to evaluate the performance of a classifier on the outer fold of cross-validation
# -------------------------------------------------------------------------------------------------------
def evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, selectedF, X, y, outerFold_features, outerFold_labels, label_names):
'''
This function is used to evaluate the performance of a classifier on the outer fold of cross-validation.
args:
clf_name: a string specifying the name of the classifier being used
clf: the name of the classifier class
clf_arguments1: a string specifying the name of one of the parameters of the classifier
clf_arguments2: a string specifying the name of another parameter of the classifier
param1: the value for the first parameter of the classifier
param2: the value for the second parameter of the classifier
selectedF: Index list of top 5 features from nested FFS on outer CV training data
X: Values of top 5 features in the dataset
y: Corresponding target variable of the top 5 features
outerFold_features: Unseen feature data from outer fold
outerFold_labels: Unseen corresponding target variable from outer fold
label_names: a list of two strings specifying the names of the two classes in the target variable for plot of confusion matrix
Returns:
No return value
'''
# Mapping features from list to array - Array starts with 0 but list doesn't
selectedF = [x - 1 for x in featuresOuterFold]
# -----------------------------------------------------------------------------------------
# Training classifier with the parameters and features with best accuracy found in inner CV
# -----------------------------------------------------------------------------------------
'''
This block of code checks if the classifier is SVM, and if it is, it sets probability to True while creating a new instance of the classifier.
It creates a dictionary of the classifier arguments (clf_arguments1 and clf_arguments2) and their corresponding values (param1 and param2),
and passes them as keyword arguments to the classifier function, creating an instance of the classifier.
If the classifier is not SVM, it creates a new instance of the classifier using the same dictionary of arguments without setting the probability parameter.
Finally, it fits the created classifier instance on the selected features (selectedF) of the training data (X) and their corresponding labels (y).
'''
if clf_name == 'SVM':
args = {clf_arguments1:param1, clf_arguments2:param2}
classifier = clf(**args, probability=True)
else:
args = {clf_arguments1:param1, clf_arguments2:param2}
classifier = clf(**args)
classifier.fit(X[:, selectedF], y)
print("--------------------------------------------------------------------------------------------------")
print("Training Score on outer fold:", round(classifier.score(X[:, selectedF], y), 6) * 100)
print("--------------------------------------------------------------------------------------------------")
# --------------------------------------------------------------------------------------------------
# Testing on Outer CV
# --------------------------------------------------------------------------------------------------
y_predict = classifier.predict(outerFold_features[:, selectedF])
print("Test Accuracy on outer fold:", round(accuracy_score(outerFold_labels, y_predict), 6) * 100)
print("--------------------------------------------------------------------------------------------------")
print(clf_arguments1 + ": " + str(param1))
print(clf_arguments2 + ": " + str(param2))
print("Features: ", featuresOuterFold)
print("--------------------------------------------------------------------------------------------------")
# --------------------------------------------------------------------------------------------------
# Classification Report
# --------------------------------------------------------------------------------------------------
print(classification_report(outerFold_labels, y_predict, digits=6))
print("--------------------------------------------------------------------------------------------------")
# --------------------------------------------------------------------------------------------------
# ROC AUC Curve and Score
# --------------------------------------------------------------------------------------------------
# Generating a list of 0 with the length of outerFold_labels
ns_probs = [0 for _ in range(len(outerFold_labels))]
# Predicting class probabilities for the test set using the classifier
lr_probs = classifier.predict_proba(outerFold_features[:, selectedF])
# Selecting only the probabilities for the positive class
lr_probs = lr_probs[:, 1]
# Calculating the ROC AUC score for a model that predicts only 0's
ns_auc = roc_auc_score(outerFold_labels, ns_probs)
# Calculating the ROC AUC score for the classifier
lr_auc = roc_auc_score(outerFold_labels, lr_probs)
# Print the no skill ROC AUC score and the ROC AUC score of the classifier
print('No Skill: ROC AUC=%.3f' % (ns_auc))
print(clf_name + ': ROC AUC=%.3f' % (lr_auc))
print("--------------------------------------------------------------------------------------------------")
# Compute the false positive rate, true positive rate and thresholds for the no skill model and the classifier
ns_fpr, ns_tpr, _ = roc_curve(outerFold_labels, ns_probs)
lr_fpr, lr_tpr, _ = roc_curve(outerFold_labels, lr_probs)
# Plot the ROC curves for the no skill model and the classifier
plt.plot(ns_fpr, ns_tpr, linestyle='--', label='No Skill')
plt.plot(lr_fpr, lr_tpr, marker='.', label=clf_name)
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.legend()
plt.title(clf_name)
plt.show()
print("--------------------------------------------------------------------------------------------------")
print("Confusion Matrix")
print("--------------------------------------------------------------------------------------------------")
ax= plt.subplot()
plt.title(clf_name)
metrics.ConfusionMatrixDisplay(
confusion_matrix = metrics.confusion_matrix(outerFold_labels, y_predict), display_labels = [label_names[0], label_names[1]]).plot(ax=ax, cmap=plt.cm.Greens);
# ------------------------------------------------------------------------------------------------------------
# These lines append various metrics and information to global variables to be used later for further analysis
# ------------------------------------------------------------------------------------------------------------
param_1.append(param1)
param_2.append(param2)
trainScore.append(round(classifier.score(X[:, selectedF], y), 6) * 100)
testScore.append(round(accuracy_score(outerFold_labels, y_predict), 6) * 100)
falsePositiveRate.append(lr_fpr)
truePositiveRate.append(lr_tpr)
aucScore.append(lr_auc)
featureSubset.append(featuresOuterFold)
def combinedROCPlot(clf_name, featureSubset, param_1, param_2, trainScore, testScore, aucScore, falsePositiveRate, truePositiveRate):
'''
This is a function that plots a combined ROC curve for a given classifier over multiple outer folds of cross-validation.
The function takes in the following parameters:
args:
clf_name: the name of the classifier being used
featureSubset: a list containing the selected feature subset for each outer fold
param_1: a list containing the value of the first hyperparameter for the classifier for each outer fold
param_2: a list containing the value of the second hyperparameter for the classifier for each outer fold
trainScore: a list containing the training score for the classifier for each outer fold
testScore: a list containing the testing score for the classifier for each outer fold
aucScore: a list containing the AUC score for the classifier for each outer fold
falsePositiveRate: a list containing the false positive rate for the ROC curve for each outer fold
truePositiveRate: a list containing the true positive rate for the ROC curve for each outer fold
'''
# Plot the No Skill line
plt.plot([0, 1], [0, 1], linestyle='--', label='No Skill')
# Iterate over each outer fold and print out the results
for i in range(0, 3):
print("--------------------------------------------")
print("Outer Fold " + str(i + 1) + " Result")
print("--------------------------------------------")
print("Feature Subset: ", featureSubset[i])
print("Best n_estimator: ", param_1[i])
print("Best max_depth: ", param_2[i])
print("Train Score: ", trainScore[i])
print("Test Score: ", testScore[i])
print("AUC Score: ", aucScore[i])
# Iterate over each outer fold and plot the ROC curve for that fold
for i in range(0, 3):
plt.plot(falsePositiveRate[i], truePositiveRate[i], marker='.', label = clf_name + ' - Outer Fold ' + str(i+1))
# Add axis labels, legend, and display the graph
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Combine ROC plots')
plt.legend()
plt.show()
# Global variables
# These lines of code are initializing several empty lists
# which will be used later in the program to store data or results
# Resetting global variable data for other classifiers
param_1 = []
param_2 = []
trainScore = []
testScore = []
falsePositiveRate = []
truePositiveRate = []
aucScore = []
featureSubset = []
# Classifier Name
clf_name = 'Random Forest'
# Classifier's class name
clf = RandomForestClassifier
# Argument 1 name for classifier
clf_arguments1 = 'n_estimators'
# Argument 2 name for classifier
clf_arguments2 = 'max_depth'
# Values of argument 1 parameter list for classifier
params1 = [[5, 3, 2], [20, 25, 30], [35, 40, 45], [50, 55, 60], [65, 80, 100]]
# max_depth parameter list for Random Forest
params2 = [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7]]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_1, label_1)
Iteration 1 >> Feature: 67; n_estimators: 65; max_depth: 7; Train Accuracy: 0.8979; Test Accuracy: 0.6617; Selected Features: [67] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 71; n_estimators: 55; max_depth: 6; Train Accuracy: 0.8468; Test Accuracy: 0.6562; Selected Features: [67, 71] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 86; n_estimators: 45; max_depth: 3; Train Accuracy: 0.7191; Test Accuracy: 0.6559; Selected Features: [67, 71, 86] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 49; n_estimators: 25; max_depth: 4; Train Accuracy: 0.7581; Test Accuracy: 0.6499; Selected Features: [67, 71, 86, 49] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 57; n_estimators: 5; max_depth: 1; Train Accuracy: 0.6412; Test Accuracy: 0.6186; Selected Features: [67, 71, 86, 49, 57] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 5
# Values of argument 2 parameter list for classifier
param2 = 1
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_1, label_1, outerFold_features_3, outerFold_labels_3, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 68.8172
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 56.383
--------------------------------------------------------------------------------------------------
n_estimators: 5
max_depth: 1
Features: [67, 71, 86, 49, 57]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.514286 0.428571 0.467532 42
1 0.593220 0.673077 0.630631 52
accuracy 0.563830 94
macro avg 0.553753 0.550824 0.549082 94
weighted avg 0.557952 0.563830 0.557757 94
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
Random Forest: ROC AUC=0.587
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
params1 = [[5, 10, 15], [20, 25, 30], [35, 40, 45], [50, 55, 60], [70,75,150]]
# max_depth parameter list for Random Forest
params2 = [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7]]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_2, label_2)
Iteration 1 >> Feature: 57; n_estimators: 75; max_depth: 7; Train Accuracy: 0.8944; Test Accuracy: 0.6529; Selected Features: [57] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 43; n_estimators: 50; max_depth: 6; Train Accuracy: 0.877; Test Accuracy: 0.6521; Selected Features: [57, 43] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 88; n_estimators: 45; max_depth: 5; Train Accuracy: 0.8088; Test Accuracy: 0.6309; Selected Features: [57, 43, 88] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 56; n_estimators: 30; max_depth: 2; Train Accuracy: 0.6751; Test Accuracy: 0.6316; Selected Features: [57, 43, 88, 56] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 86; n_estimators: 5; max_depth: 3; Train Accuracy: 0.6938; Test Accuracy: 0.6358; Selected Features: [57, 43, 88, 56, 86] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 45
# Values of argument 2 parameter list for classifier
param2 = 5
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_2, label_2, outerFold_features_2, outerFold_labels_2, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 91.4439
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 55.913999999999994
--------------------------------------------------------------------------------------------------
n_estimators: 45
max_depth: 5
Features: [57, 43, 88, 56, 86]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.588235 0.425532 0.493827 47
1 0.542373 0.695652 0.609524 46
accuracy 0.559140 93
macro avg 0.565304 0.560592 0.551675 93
weighted avg 0.565551 0.559140 0.551053 93
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
Random Forest: ROC AUC=0.574
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
params1 = [[3, 10, 5], [4, 25, 30], [35, 8, 45], [40, 55, 60], [200,90, 100]]
# max_depth parameter list for Random Forest
params2 = [[1, 2, 3], [2, 5, 4], [3, 4, 5], [4, 5, 6], [4, 8, 7]]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_3, label_3)
Iteration 1 >> Feature: 68; n_estimators: 200; max_depth: 7; Train Accuracy: 0.9131; Test Accuracy: 0.6694; Selected Features: [68] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 79; n_estimators: 55; max_depth: 4; Train Accuracy: 0.7713; Test Accuracy: 0.6686; Selected Features: [68, 79] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 95; n_estimators: 8; max_depth: 4; Train Accuracy: 0.75; Test Accuracy: 0.6522; Selected Features: [68, 79, 95] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 26; n_estimators: 4; max_depth: 5; Train Accuracy: 0.7513; Test Accuracy: 0.6361; Selected Features: [68, 79, 95, 26] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 1; n_estimators: 5; max_depth: 1; Train Accuracy: 0.6404; Test Accuracy: 0.6366; Selected Features: [68, 79, 95, 26, 1] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 4
# Values of argument 2 parameter list for classifier
param2 = 5
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_3, label_3, outerFold_features_1, outerFold_labels_1, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 80.7487
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 45.1613
--------------------------------------------------------------------------------------------------
n_estimators: 4
max_depth: 5
Features: [68, 79, 95, 26, 1]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.480000 0.489796 0.484848 49
1 0.418605 0.409091 0.413793 44
accuracy 0.451613 93
macro avg 0.449302 0.449443 0.449321 93
weighted avg 0.450953 0.451613 0.451231 93
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
Random Forest: ROC AUC=0.407
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
combinedROCPlot(clf_name, featureSubset, param_1, param_2, trainScore, testScore, aucScore, falsePositiveRate, truePositiveRate)
-------------------------------------------- Outer Fold 1 Result -------------------------------------------- Feature Subset: [67, 71, 86, 49, 57] Best n_estimator: 5 Best max_depth: 1 Train Score: 68.8172 Test Score: 56.383 AUC Score: 0.5872252747252747 -------------------------------------------- Outer Fold 2 Result -------------------------------------------- Feature Subset: [57, 43, 88, 56, 86] Best n_estimator: 5 Best max_depth: 3 Train Score: 71.123 Test Score: 49.4624 AUC Score: 0.5795559666975023 -------------------------------------------- Outer Fold 3 Result -------------------------------------------- Feature Subset: [57, 43, 88, 56, 86] Best n_estimator: 45 Best max_depth: 5 Train Score: 91.4439 Test Score: 55.913999999999994 AUC Score: 0.574468085106383
listB = [78, 68, 48, 59, 61]
res = list(map(selectedFeatures.__getitem__, listB))
res.append('BlcaGrade')
sns.pairplot(data[res], hue='BlcaGrade', palette='tab10')
<seaborn.axisgrid.PairGrid at 0x7f5e96785fa0>
# Global variables
# These lines of code are initializing several empty lists
# which will be used later in the program to store data or results
# Resetting global variable data for other classifiers
param_1 = []
param_2 = []
trainScore = []
testScore = []
falsePositiveRate = []
truePositiveRate = []
aucScore = []
featureSubset = []
# Classifier Name
clf_name = 'SVM'
# Classifier's class name
clf = SVC
# Argument 1 name for classifier
clf_arguments1 = 'C'
# Argument 2 name for classifier
clf_arguments2 = 'kernel'
# Values of argument 1 parameter list for classifier
params1 = [[0.01, 0.1], [0.001,0.2], [0.002, 0.3], [0.02, 0.003 ], [0.0001,2]]
# Values of argument 2 parameter list for classifier
params2 = [['rbf'], ['rbf'], ['rbf'], ['rbf'], ['rbf']]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_1, label_1)
Iteration 1 >> Feature: 57; C: 2; kernel: rbf; Train Accuracy: 0.6331; Test Accuracy: 0.624; Selected Features: [57] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 100; C: 0.003; kernel: rbf; Train Accuracy: 0.5161; Test Accuracy: 0.5161; Selected Features: [57, 100] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 39; C: 0.3; kernel: rbf; Train Accuracy: 0.617; Test Accuracy: 0.603; Selected Features: [57, 100, 39] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 54; C: 0.2; kernel: rbf; Train Accuracy: 0.5873; Test Accuracy: 0.5642; Selected Features: [57, 100, 39, 54] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 97; C: 0.1; kernel: rbf; Train Accuracy: 0.5457; Test Accuracy: 0.5533; Selected Features: [57, 100, 39, 54, 97] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 2
# Values of argument 2 parameter list for classifier
param2 = 'rbf'
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_1, label_1, outerFold_features_3, outerFold_labels_3, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 65.5914
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 46.808499999999995
--------------------------------------------------------------------------------------------------
C: 2
kernel: rbf
Features: [57, 100, 39, 54, 97]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.416667 0.476190 0.444444 42
1 0.521739 0.461538 0.489796 52
accuracy 0.468085 94
macro avg 0.469203 0.468864 0.467120 94
weighted avg 0.474792 0.468085 0.469532 94
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
SVM: ROC AUC=0.433
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
params1 = [[0.01,0.0001], [0.001,1], [0.001, 0.1], [0.002, 0.2], [0.001, 0.002]]
# Values of argument 2 parameter list for classifier
params2 = [['rbf'], ['rbf'], ['rbf'], ['rbf'], ['rbf']]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_2, label_2)
Iteration 1 >> Feature: 100; C: 0.002; kernel: rbf; Train Accuracy: 0.5134; Test Accuracy: 0.5134; Selected Features: [100] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 53; C: 0.2; kernel: rbf; Train Accuracy: 0.603; Test Accuracy: 0.5945; Selected Features: [100, 53] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 25; C: 0.1; kernel: rbf; Train Accuracy: 0.5976; Test Accuracy: 0.5832; Selected Features: [100, 53, 25] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 56; C: 1; kernel: rbf; Train Accuracy: 0.619; Test Accuracy: 0.599; Selected Features: [100, 53, 25, 56] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 99; C: 0.0001; kernel: rbf; Train Accuracy: 0.5134; Test Accuracy: 0.5134; Selected Features: [100, 53, 25, 56, 99] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 0.1
# Values of argument 2 parameter list for classifier
param2 = 'rbf'
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_2, label_2, outerFold_features_2, outerFold_labels_2, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 56.68450000000001
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 50.537600000000005
--------------------------------------------------------------------------------------------------
C: 0.1
kernel: rbf
Features: [100, 53, 25, 56, 99]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.521739 0.255319 0.342857 47
1 0.500000 0.760870 0.603448 46
accuracy 0.505376 93
macro avg 0.510870 0.508094 0.473153 93
weighted avg 0.510986 0.505376 0.471752 93
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
SVM: ROC AUC=0.462
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
params1 = [[0.01,0.002], [0.003,0.1], [0.02,0.01], [0.001,0.3], [0.03, 1]]
# Values of argument 2 parameter list for classifier
params2 = [['rbf'], ['rbf'], ['rbf'], ['rbf'], ['rbf']]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_3, label_3)
Iteration 1 >> Feature: 1; C: 1; kernel: rbf; Train Accuracy: 0.6137; Test Accuracy: 0.6149; Selected Features: [1] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 19; C: 0.3; kernel: rbf; Train Accuracy: 0.5949; Test Accuracy: 0.5828; Selected Features: [1, 19] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 100; C: 0.01; kernel: rbf; Train Accuracy: 0.5241; Test Accuracy: 0.524; Selected Features: [1, 19, 100] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 25; C: 0.1; kernel: rbf; Train Accuracy: 0.5963; Test Accuracy: 0.5936; Selected Features: [1, 19, 100, 25] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 99; C: 0.002; kernel: rbf; Train Accuracy: 0.5241; Test Accuracy: 0.524; Selected Features: [1, 19, 100, 25, 99] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 1
# Values of argument 2 parameter list for classifier
param2 = 'rbf'
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_3, label_3, outerFold_features_1, outerFold_labels_1, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 60.4278
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 50.537600000000005
--------------------------------------------------------------------------------------------------
C: 1
kernel: rbf
Features: [1, 19, 100, 25, 99]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.545455 0.367347 0.439024 49
1 0.483333 0.659091 0.557692 44
accuracy 0.505376 93
macro avg 0.514394 0.513219 0.498358 93
weighted avg 0.516064 0.505376 0.495168 93
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
SVM: ROC AUC=0.508
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
combinedROCPlot(clf_name, featureSubset, param_1, param_2, trainScore, testScore, aucScore, falsePositiveRate, truePositiveRate)
-------------------------------------------- Outer Fold 1 Result -------------------------------------------- Feature Subset: [57, 100, 39, 54, 97] Best n_estimator: 2 Best max_depth: rbf Train Score: 65.5914 Test Score: 46.808499999999995 AUC Score: 0.43315018315018317 -------------------------------------------- Outer Fold 2 Result -------------------------------------------- Feature Subset: [100, 53, 25, 56, 99] Best n_estimator: 0.1 Best max_depth: rbf Train Score: 56.68450000000001 Test Score: 50.537600000000005 AUC Score: 0.46230342275670677 -------------------------------------------- Outer Fold 3 Result -------------------------------------------- Feature Subset: [1, 19, 100, 25, 99] Best n_estimator: 1 Best max_depth: rbf Train Score: 60.4278 Test Score: 50.537600000000005 AUC Score: 0.5078849721706865
listB = [78, 68, 48, 59, 61]
res = list(map(selectedFeatures.__getitem__, listB))
res.append('BlcaGrade')
sns.pairplot(data[res], hue='BlcaGrade', palette='tab10')
<seaborn.axisgrid.PairGrid at 0x7f9bff6eba30>
# Global variables
# These lines of code are initializing several empty lists
# which will be used later in the program to store data or results
# Resetting global variable data for other classifiers
param_1 = []
param_2 = []
trainScore = []
testScore = []
falsePositiveRate = []
truePositiveRate = []
aucScore = []
featureSubset = []
# Classifier Name
clf_name = 'Gradient Boosting'
# Classifier's class name
clf = GradientBoostingClassifier
# Argument 1 name for classifier
clf_arguments1 = 'n_estimators'
# Argument 2 name for classifier
clf_arguments2 = 'max_depth'
# Values of argument 1 parameter list for classifier
params1 = [[3, 5], [10, 25], [30, 35], [50, 60], [70, 100]]
# Values of argument 2 parameter list for classifier
params2 = [[1, 2], [2, 3], [3, 4], [4, 6], [5, 7]]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_1, label_1)
Iteration 1 >> Feature: 59; n_estimators: 100; max_depth: 7; Train Accuracy: 1.0; Test Accuracy: 0.6347; Selected Features: [59] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 32; n_estimators: 60; max_depth: 4; Train Accuracy: 0.957; Test Accuracy: 0.6344; Selected Features: [59, 32] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 86; n_estimators: 30; max_depth: 3; Train Accuracy: 0.7836; Test Accuracy: 0.6558; Selected Features: [59, 32, 86] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 49; n_estimators: 25; max_depth: 2; Train Accuracy: 0.7446; Test Accuracy: 0.6607; Selected Features: [59, 32, 86, 49] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 57; n_estimators: 5; max_depth: 2; Train Accuracy: 0.6586; Test Accuracy: 0.6132; Selected Features: [59, 32, 86, 49, 57] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 5
# Values of argument 2 parameter list for classifier
param2 = 2
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_1, label_1, outerFold_features_3, outerFold_labels_3, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 74.7312
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 54.2553
--------------------------------------------------------------------------------------------------
n_estimators: 5
max_depth: 2
Features: [59, 32, 86, 49, 57]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.491803 0.714286 0.582524 42
1 0.636364 0.403846 0.494118 52
accuracy 0.542553 94
macro avg 0.564083 0.559066 0.538321 94
weighted avg 0.571773 0.542553 0.533618 94
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
Gradient Boosting: ROC AUC=0.556
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
params1 = [[5, 2], [3, 25], [3, 35], [80, 90], [100, 150]]
# Values of argument 2 parameter list for classifier
params2 = [[3, 5], [7, 4], [6, 3], [1, 6], [5, 2]]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_2, label_2)
Iteration 1 >> Feature: 43; n_estimators: 150; max_depth: 5; Train Accuracy: 1.0; Test Accuracy: 0.6414; Selected Features: [43] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 56; n_estimators: 90; max_depth: 1; Train Accuracy: 0.6858; Test Accuracy: 0.6418; Selected Features: [43, 56] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 88; n_estimators: 35; max_depth: 6; Train Accuracy: 0.9666; Test Accuracy: 0.6368; Selected Features: [43, 56, 88] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 69; n_estimators: 25; max_depth: 7; Train Accuracy: 0.9906; Test Accuracy: 0.6367; Selected Features: [43, 56, 88, 69] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 86; n_estimators: 5; max_depth: 5; Train Accuracy: 0.7352; Test Accuracy: 0.6252; Selected Features: [43, 56, 88, 69, 86] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 5
# Values of argument 2 parameter list for classifier
param2 = 5
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_2, label_2, outerFold_features_2, outerFold_labels_2, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 85.5615
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 52.688199999999995
--------------------------------------------------------------------------------------------------
n_estimators: 5
max_depth: 5
Features: [43, 56, 88, 69, 86]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.551724 0.340426 0.421053 47
1 0.515625 0.717391 0.600000 46
accuracy 0.526882 93
macro avg 0.533675 0.528908 0.510526 93
weighted avg 0.533869 0.526882 0.509564 93
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
Gradient Boosting: ROC AUC=0.485
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
params1 = [[1, 2], [25, 3], [50, 4], [40, 5], [30, 10]]
# Values of argument 2 parameter list for classifier
params2 = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_3, label_3)
Iteration 1 >> Feature: 68; n_estimators: 30; max_depth: 6; Train Accuracy: 0.9759; Test Accuracy: 0.6585; Selected Features: [68] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 79; n_estimators: 5; max_depth: 5; Train Accuracy: 0.8048; Test Accuracy: 0.6471; Selected Features: [68, 79] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 82; n_estimators: 4; max_depth: 3; Train Accuracy: 0.6872; Test Accuracy: 0.6363; Selected Features: [68, 79, 82] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 98; n_estimators: 25; max_depth: 2; Train Accuracy: 0.738; Test Accuracy: 0.6368; Selected Features: [68, 79, 82, 98] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 12; n_estimators: 2; max_depth: 1; Train Accuracy: 0.6096; Test Accuracy: 0.5991; Selected Features: [68, 79, 82, 98, 12] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 4
# Values of argument 2 parameter list for classifier
param2 = 3
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_3, label_3, outerFold_features_1, outerFold_labels_1, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 70.5882
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 48.3871
--------------------------------------------------------------------------------------------------
n_estimators: 4
max_depth: 3
Features: [68, 79, 82, 98, 12]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.518519 0.285714 0.368421 49
1 0.469697 0.704545 0.563636 44
accuracy 0.483871 93
macro avg 0.494108 0.495130 0.466029 93
weighted avg 0.495420 0.483871 0.460781 93
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
Gradient Boosting: ROC AUC=0.503
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
combinedROCPlot(clf_name, featureSubset, param_1, param_2, trainScore, testScore, aucScore, falsePositiveRate, truePositiveRate)
-------------------------------------------- Outer Fold 1 Result -------------------------------------------- Feature Subset: [59, 32, 86, 49, 57] Best n_estimator: 5 Best max_depth: 2 Train Score: 74.7312 Test Score: 54.2553 AUC Score: 0.5563186813186813 -------------------------------------------- Outer Fold 2 Result -------------------------------------------- Feature Subset: [43, 56, 88, 69, 86] Best n_estimator: 5 Best max_depth: 5 Train Score: 85.5615 Test Score: 52.688199999999995 AUC Score: 0.48519888991674376 -------------------------------------------- Outer Fold 3 Result -------------------------------------------- Feature Subset: [68, 79, 82, 98, 12] Best n_estimator: 4 Best max_depth: 3 Train Score: 70.5882 Test Score: 48.3871 AUC Score: 0.5032467532467533
listB = [66, 33, 84, 55, 99]
res = list(map(selectedFeatures.__getitem__, listB))
res.append('BlcaGrade')
sns.pairplot(data[res], hue='BlcaGrade', palette='tab10')
<seaborn.axisgrid.PairGrid at 0x7f5e9b52dbb0>
# Global variables
# These lines of code are initializing several empty lists
# which will be used later in the program to store data or results
# Resetting global variable data for other classifiers
param_1 = []
param_2 = []
trainScore = []
testScore = []
falsePositiveRate = []
truePositiveRate = []
aucScore = []
featureSubset = []
# Classifier Name
clf_name = 'KNN'
# Classifier's class name
clf = KNeighborsClassifier
# Argument 1 name for classifier
clf_arguments1 = 'n_neighbors'
# Argument 2 name for classifier
clf_arguments2 = 'weights'
# Values of argument 1 parameter list for classifier
params1 = [[2, 3], [1, 4], [4, 5], [5, 6], [6, 7]]
# Values of argument 2 parameter list for classifier
params2 = [['uniform', 'distance'], ['uniform', 'distance'], ['uniform', 'distance'], ['uniform', 'distance'], ['uniform', 'distance']]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_1, label_1)
Iteration 1 >> Feature: 71; n_neighbors: 7; weights: uniform; Train Accuracy: 0.7164; Test Accuracy: 0.64; Selected Features: [71] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 86; n_neighbors: 5; weights: uniform; Train Accuracy: 0.7419; Test Accuracy: 0.6616; Selected Features: [71, 86] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 49; n_neighbors: 5; weights: uniform; Train Accuracy: 0.7298; Test Accuracy: 0.6609; Selected Features: [71, 86, 49] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 67; n_neighbors: 4; weights: distance; Train Accuracy: 1.0; Test Accuracy: 0.6506; Selected Features: [71, 86, 49, 67] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 18; n_neighbors: 2; weights: uniform; Train Accuracy: 0.7728; Test Accuracy: 0.6772; Selected Features: [71, 86, 49, 67, 18] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 2
# Values of argument 2 parameter list for classifier
param2 = 'uniform'
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_1, label_1, outerFold_features_3, outerFold_labels_3, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 77.957
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 44.6809
--------------------------------------------------------------------------------------------------
n_neighbors: 2
weights: uniform
Features: [71, 86, 49, 67, 18]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.426471 0.690476 0.527273 42
1 0.500000 0.250000 0.333333 52
accuracy 0.446809 94
macro avg 0.463235 0.470238 0.430303 94
weighted avg 0.467146 0.446809 0.419987 94
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
KNN: ROC AUC=0.458
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
params1 = [[2, 5], [3, 7], [5, 3], [9, 3], [7,25]]
# Values of argument 2 parameter list for classifier
params2 = [['uniform', 'distance'], ['uniform', 'distance'], ['uniform', 'distance'], ['uniform', 'distance'], ['uniform', 'distance']]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_2, label_2)
Iteration 1 >> Feature: 98; n_neighbors: 25; weights: distance; Train Accuracy: 1.0; Test Accuracy: 0.6474; Selected Features: [98] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 55; n_neighbors: 9; weights: distance; Train Accuracy: 0.9947; Test Accuracy: 0.6523; Selected Features: [98, 55] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 43; n_neighbors: 3; weights: distance; Train Accuracy: 1.0; Test Accuracy: 0.6467; Selected Features: [98, 55, 43] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 69; n_neighbors: 3; weights: distance; Train Accuracy: 1.0; Test Accuracy: 0.637; Selected Features: [98, 55, 43, 69] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 88; n_neighbors: 5; weights: distance; Train Accuracy: 0.9839; Test Accuracy: 0.6417; Selected Features: [98, 55, 43, 69, 88] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 5
# Values of argument 2 parameter list for classifier
param2 = 'distance'
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_2, label_2, outerFold_features_2, outerFold_labels_2, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 100.0
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 49.4624
--------------------------------------------------------------------------------------------------
n_neighbors: 5
weights: distance
Features: [98, 55, 43, 69, 88]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.500000 0.425532 0.459770 47
1 0.490566 0.565217 0.525253 46
accuracy 0.494624 93
macro avg 0.495283 0.495375 0.492511 93
weighted avg 0.495334 0.494624 0.492159 93
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
KNN: ROC AUC=0.513
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
params1 = [[2, 3], [3, 4], [4, 5], [5, 6], [6, 7]]
# Values of argument 2 parameter list for classifier
params2 = [['uniform', 'distance'], ['uniform', 'distance'], ['uniform', 'distance'], ['uniform', 'distance'], ['uniform', 'distance']]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_3, label_3)
Iteration 1 >> Feature: 79; n_neighbors: 6; weights: uniform; Train Accuracy: 0.7193; Test Accuracy: 0.6583; Selected Features: [79] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 1; n_neighbors: 6; weights: uniform; Train Accuracy: 0.7206; Test Accuracy: 0.6367; Selected Features: [79, 1] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 68; n_neighbors: 4; weights: distance; Train Accuracy: 1.0; Test Accuracy: 0.6374; Selected Features: [79, 1, 68] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 21; n_neighbors: 3; weights: uniform; Train Accuracy: 0.7794; Test Accuracy: 0.6414; Selected Features: [79, 1, 68, 21] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 6; n_neighbors: 3; weights: uniform; Train Accuracy: 0.7888; Test Accuracy: 0.6366; Selected Features: [79, 1, 68, 21, 6] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 6
# Values of argument 2 parameter list for classifier
param2 = 'uniform'
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_3, label_3, outerFold_features_1, outerFold_labels_1, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 72.7273
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 50.537600000000005
--------------------------------------------------------------------------------------------------
n_neighbors: 6
weights: uniform
Features: [79, 1, 68, 21, 6]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.530612 0.530612 0.530612 49
1 0.477273 0.477273 0.477273 44
accuracy 0.505376 93
macro avg 0.503942 0.503942 0.503942 93
weighted avg 0.505376 0.505376 0.505376 93
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
KNN: ROC AUC=0.490
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
combinedROCPlot(clf_name, featureSubset, param_1, param_2, trainScore, testScore, aucScore, falsePositiveRate, truePositiveRate)
-------------------------------------------- Outer Fold 1 Result -------------------------------------------- Feature Subset: [71, 86, 49, 67, 18] Best n_estimator: 2 Best max_depth: uniform Train Score: 77.957 Test Score: 44.6809 AUC Score: 0.45787545787545786 -------------------------------------------- Outer Fold 2 Result -------------------------------------------- Feature Subset: [55, 83, 43, 69, 88] Best n_estimator: 10 Best max_depth: distance Train Score: 100.0 Test Score: 52.688199999999995 AUC Score: 0.5101757631822387 -------------------------------------------- Outer Fold 3 Result -------------------------------------------- Feature Subset: [55, 83, 43, 69, 88] Best n_estimator: 5 Best max_depth: distance Train Score: 100.0 Test Score: 49.4624 AUC Score: 0.4942183163737281
listB = [79, 1, 68, 21, 6]
res = list(map(selectedFeatures.__getitem__, listB))
res.append('BlcaGrade')
sns.pairplot(data[res], hue='BlcaGrade', palette='tab10')
<seaborn.axisgrid.PairGrid at 0x7f9befcc8eb0>
# Global variables
# These lines of code are initializing several empty lists
# which will be used later in the program to store data or results
# Resetting global variable data for other classifiers
param_1 = []
param_2 = []
trainScore = []
testScore = []
falsePositiveRate = []
truePositiveRate = []
aucScore = []
featureSubset = []
# Classifier Name
clf_name = 'Logistic Regression'
# Classifier's class name
clf = LogisticRegression
# Argument 1 name for classifier
clf_arguments1 = 'solver'
# Argument 2 name for classifier
clf_arguments2 = 'penalty'
# Values of argument 1 parameter list for classifier
params1 = [['lbfgs', 'liblinear'], ['lbfgs', 'liblinear'], ['lbfgs', 'liblinear'], ['lbfgs', 'liblinear'], ['lbfgs', 'liblinear']]
# Values of argument 2 parameter list for classifier
params2 = [['l2'], ['l2'], ['l2'], [ 'l2'], ['l2']]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_1, label_1)
Iteration 1 >> Feature: 57; solver: lbfgs; penalty: l2; Train Accuracy: 0.6317; Test Accuracy: 0.6347; Selected Features: [57] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 39; solver: lbfgs; penalty: l2; Train Accuracy: 0.617; Test Accuracy: 0.6245; Selected Features: [57, 39] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 84; solver: lbfgs; penalty: l2; Train Accuracy: 0.5658; Test Accuracy: 0.5912; Selected Features: [57, 39, 84] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 54; solver: lbfgs; penalty: l2; Train Accuracy: 0.5779; Test Accuracy: 0.575; Selected Features: [57, 39, 84, 54] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 82; solver: lbfgs; penalty: l2; Train Accuracy: 0.5457; Test Accuracy: 0.5706; Selected Features: [57, 39, 84, 54, 82] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 'lbfgs'
# Values of argument 2 parameter list for classifier
param2 = 'l2'
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_1, label_1, outerFold_features_3, outerFold_labels_3, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 67.20429999999999
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 40.4255
--------------------------------------------------------------------------------------------------
solver: lbfgs
penalty: l2
Features: [57, 39, 84, 54, 82]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.333333 0.333333 0.333333 42
1 0.461538 0.461538 0.461538 52
accuracy 0.404255 94
macro avg 0.397436 0.397436 0.397436 94
weighted avg 0.404255 0.404255 0.404255 94
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
Logistic Regression: ROC AUC=0.430
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
params1 = [['lbfgs', 'liblinear'], ['lbfgs', 'sag'], ['lbfgs', 'sag'], ['lbfgs', 'sag'], ['lbfgs', 'sag']]
# Values of argument 2 parameter list for classifier
params2 = [['l2'], ['l2'], ['l2'], ['l2'], ['l2']]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_2, label_2)
Iteration 1 >> Feature: 25; solver: lbfgs; penalty: l2; Train Accuracy: 0.5923; Test Accuracy: 0.5993; Selected Features: [25] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 53; solver: lbfgs; penalty: l2; Train Accuracy: 0.5575; Test Accuracy: 0.5836; Selected Features: [25, 53] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 33; solver: lbfgs; penalty: l2; Train Accuracy: 0.5869; Test Accuracy: 0.5667; Selected Features: [25, 53, 33] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 59; solver: lbfgs; penalty: l2; Train Accuracy: 0.5655; Test Accuracy: 0.5617; Selected Features: [25, 53, 33, 59] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 99; solver: liblinear; penalty: l2; Train Accuracy: 0.5641; Test Accuracy: 0.5613; Selected Features: [25, 53, 33, 59, 99] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 'lbfgs'
# Values of argument 2 parameter list for classifier
param2 = 'l2'
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_2, label_2, outerFold_features_2, outerFold_labels_2, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 54.545500000000004
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 50.537600000000005
--------------------------------------------------------------------------------------------------
solver: lbfgs
penalty: l2
Features: [25, 53, 33, 59, 99]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.514286 0.382979 0.439024 47
1 0.500000 0.630435 0.557692 46
accuracy 0.505376 93
macro avg 0.507143 0.506707 0.498358 93
weighted avg 0.507220 0.505376 0.497720 93
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
Logistic Regression: ROC AUC=0.470
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
params1 = [['lbfgs', 'liblinear'], ['lbfgs', 'liblinear'], ['lbfgs', 'liblinear'], ['lbfgs', 'liblinear'], ['lbfgs', 'liblinear']]
# Values of argument 2 parameter list for classifier
params2 = [['l2'], ['l2'], ['l2'], ['l2'], ['l2']]
# Empty list for FFS - Global Variable
featuresOuterFold = []
# Invoking FFS
outerFold(clf, clf_arguments1, clf_arguments2, params1, params2, features_3, label_3)
Iteration 1 >> Feature: 25; solver: liblinear; penalty: l2; Train Accuracy: 0.5963; Test Accuracy: 0.5991; Selected Features: [25] --------------------------------------------------------------------------------------------------------------------------------- Iteration 2 >> Feature: 70; solver: liblinear; penalty: l2; Train Accuracy: 0.5829; Test Accuracy: 0.5989; Selected Features: [25, 70] --------------------------------------------------------------------------------------------------------------------------------- Iteration 3 >> Feature: 73; solver: lbfgs; penalty: l2; Train Accuracy: 0.5802; Test Accuracy: 0.5935; Selected Features: [25, 70, 73] --------------------------------------------------------------------------------------------------------------------------------- Iteration 4 >> Feature: 1; solver: liblinear; penalty: l2; Train Accuracy: 0.5441; Test Accuracy: 0.567; Selected Features: [25, 70, 73, 1] --------------------------------------------------------------------------------------------------------------------------------- Iteration 5 >> Feature: 13; solver: liblinear; penalty: l2; Train Accuracy: 0.5561; Test Accuracy: 0.567; Selected Features: [25, 70, 73, 1, 13] ---------------------------------------------------------------------------------------------------------------------------------
# Values of argument 1 parameter list for classifier
param1 = 'lbfgs'
# Values of argument 2 parameter list for classifier
param2 = 'l2'
evaluationOuterFold(clf_name, clf, clf_arguments1, clf_arguments2, param1, param2, featuresOuterFold, features_3, label_3, outerFold_features_1, outerFold_labels_1, label_names)
--------------------------------------------------------------------------------------------------
Training Score on outer fold: 68.4492
--------------------------------------------------------------------------------------------------
Test Accuracy on outer fold: 48.3871
--------------------------------------------------------------------------------------------------
solver: lbfgs
penalty: l2
Features: [25, 70, 73, 1, 13]
--------------------------------------------------------------------------------------------------
precision recall f1-score support
0 0.512195 0.428571 0.466667 49
1 0.461538 0.545455 0.500000 44
accuracy 0.483871 93
macro avg 0.486867 0.487013 0.483333 93
weighted avg 0.488229 0.483871 0.482437 93
--------------------------------------------------------------------------------------------------
No Skill: ROC AUC=0.500
Logistic Regression: ROC AUC=0.451
--------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- Confusion Matrix --------------------------------------------------------------------------------------------------
combinedROCPlot(clf_name, featureSubset, param_1, param_2, trainScore, testScore, aucScore, falsePositiveRate, truePositiveRate)
-------------------------------------------- Outer Fold 1 Result -------------------------------------------- Feature Subset: [57, 39, 84, 54, 82] Best n_estimator: lbfgs Best max_depth: l2 Train Score: 67.20429999999999 Test Score: 40.4255 AUC Score: 0.42994505494505497 -------------------------------------------- Outer Fold 2 Result -------------------------------------------- Feature Subset: [25, 53, 33, 59, 99] Best n_estimator: lbfgs Best max_depth: l2 Train Score: 54.545500000000004 Test Score: 50.537600000000005 AUC Score: 0.46993524514338575 -------------------------------------------- Outer Fold 3 Result -------------------------------------------- Feature Subset: [25, 70, 73, 1, 13] Best n_estimator: lbfgs Best max_depth: l2 Train Score: 68.4492 Test Score: 48.3871 AUC Score: 0.450834879406308
listB = [78, 68, 48, 59, 61]
res = list(map(selectedFeatures.__getitem__, listB))
res.append('BlcaGrade')
sns.pairplot(data[res], hue='BlcaGrade', palette='tab10')